package com.tansun.easycare.rule.deployonline.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.deployonline.dao.ReflactDataDao;
import com.tansun.easycare.rule.deployonline.entity.ReflactData;
import com.tansun.easycare.rule.deployonline.entity.RuleDeployment;
import com.tansun.easycare.rule.deployonline.model.ReflactDataVO;
import com.tansun.easycare.rule.deployonline.service.ReflactDataService;
import com.tansun.easycare.rule.exception.BizException;



@Service
public class ReflactDataServiceImpl extends RuleBaseServiceImpl<ReflactDataDao, ReflactData> implements  ReflactDataService {

	
	@Override
	public void saveOrUpdate(ReflactDataVO reflactDataVO,RuleDeployment deployment) throws BizException {
		List<ReflactData> reflactDataList = reflactDataVO.getReflactDataList();
		for(ReflactData reflactData:reflactDataList) {
			if(!StringUtils.isNotEmpty(reflactData.getDeployId())) {
				reflactData.setDeployId(deployment.getId());
			}
			if(!StringUtils.isNotEmpty(reflactData.getManageId())) {
				reflactData.setManageId(deployment.getManageId());
			}
			if(!StringUtils.isNotEmpty(reflactData.getEnterPonit())) {
				reflactData.setEnterPonit(deployment.getEnterPoints());
			}
			if(!StringUtils.isNotEmpty(reflactData.getProjectCode())) {
				reflactData.setProjectCode(deployment.getProjectCode());
			}
			this.save(reflactData);
			
		}
	}

	public void insertOrUpdate(ReflactDataVO reflactDataVO,RuleDeployment deployment) {
		List<ReflactData> reflactDataList = reflactDataVO.getReflactDataList();
		for(ReflactData reflactData:reflactDataList) {
			if(StringUtils.isEmpty(reflactData.getDeployId())) {
				reflactData.setDeployId(deployment.getId());
			}
			if(StringUtils.isEmpty(reflactData.getManageId())) {
				reflactData.setManageId(deployment.getManageId());
			}
			if(StringUtils.isEmpty(reflactData.getProjectCode())) {
				reflactData.setProjectCode(deployment.getProjectCode());
			}
			if(reflactData.getIsNewRecord()) {
				this.dao.insert(reflactData);
			}else {
				this.dao.update(reflactData);
			}
		}
	}
	
	@Override
	public ReflactDataVO findReflactData(RuleDeployment deployment) throws BizException {
		ReflactData entity = new ReflactData();
		entity.setDeployId(deployment.getId());
		List<ReflactData> reflactDataList = this.findList(entity);
		ReflactDataVO reflactDataVO = new ReflactDataVO();
		reflactDataVO.setReflactDataList(reflactDataList);
		reflactDataVO.setDeploymentId(deployment.getId());
		reflactDataVO.setProjectCode(deployment.getProjectCode());
		
		return reflactDataVO;
	}

	@Override
	public void deleteReflactData(RuleDeployment deployment) throws BizException {
		ReflactData entity = new ReflactData();
		entity.setDeployId(deployment.getId());
		this.delete(entity);
	}
	
}
